package com.example.smartcitymovie.ui.home;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.smartcitymovie.MyApplication;
import com.example.smartcitymovie.bean.NewsListBean;
import com.example.smartcitymovie.bean.ResultBean;
import com.example.smartcitymovie.databinding.ActivityNewsMessageBinding;
import com.example.smartcitymovie.utils.OkHttpUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class NewsMessageActivity extends AppCompatActivity {

    ActivityNewsMessageBinding binding;
    private NewsListBean.RowsBean bean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityNewsMessageBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        getSupportActionBar().setTitle("星闻详情");
        bean = (NewsListBean.RowsBean) getIntent().getBundleExtra("bundle").getSerializable("bean");
        initView();
    }

    private void initView() {
        if (bean!=null){
            Glide.with(binding.IVMovieNewsCover).load(OkHttpUtils.BaseUrl+bean.getCover()).into(binding.IVMovieNewsCover);
            binding.TVMovieNewsThumbNum.setText(String.valueOf(bean.getLikeNum()));
            binding.TVMovieNewsTitle.setText(bean.getTitle());
            binding.WVShowNewsContent.loadDataWithBaseURL(OkHttpUtils.BaseUrl,bean.getContent(),"text/html","UTF-8",null);
            binding.IVBtnThumb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (MyApplication.getSharedPreferences().getBoolean("isLogin",false)){
                        OkHttpUtils.postCall("/prod-api/api/movie/film/comment/like/"+bean.getId()).enqueue(new Callback() {
                            @Override
                            public void onFailure(Call call, IOException e) {

                            }

                            @Override
                            public void onResponse(Call call, Response response) throws IOException {
                                ResultBean resultBean = MyApplication.getGson().fromJson(response.body().string(), ResultBean.class);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (resultBean.getCode()==200){
                                            Toast.makeText(NewsMessageActivity.this, "点赞成功", Toast.LENGTH_SHORT).show();
                                        }else{
                                            Toast.makeText(NewsMessageActivity.this, resultBean.getMsg(), Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }
                        });
                    }else{
                        Toast.makeText(NewsMessageActivity.this, "请先前往登录", Toast.LENGTH_SHORT).show();
                    }
                }
            });
            binding.TVDoComment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(NewsMessageActivity.this, "待开发", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}